package com.tianci.appstore.ui.list;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.coocaa.x.app.framework.app.LiteAppApplication;
import com.coocaa.x.framework.utils.LogUtils;
import com.skyworth.framework.skysdk.ipc.SkyStartActivity;
import com.tianci.appstore.AppDetailActivity;
import com.tianci.appstore.R;
import com.tianci.appstore.ui.data.APKData;
import com.tianci.appstore.ui.data.SkyAppBaseData;
import com.tianci.appstore.ui.data.SkyAppDetailData.AppStatus;
import com.tianci.appstore.ui.data.SkyListAppData.ListType;
import com.tianci.appstore.ui.data.SkyListStatusData;
import com.tianci.appstore.ui.util.UICommon;
import com.tianci.appstore.ui.widget.TipToast;
import com.tianci.appstore.utils.ImageUtils;
import com.tianci.appstore.utils.SkyConstants;

import java.util.HashMap;

/**
 * 列表页中的app块View
 * 使用此view的列表有：分类列表
 **/

public class ListStatusBlock extends ListBlock {
    protected TextView statusText;
    protected AppStatus status;

    protected TextView sndInfoText;
    protected TextView installedText;
    protected static final int installedTextSize = SkyConstants.Dpi(20);

    public ListStatusBlock(Context context, ListType listType) {
        super(context, listType);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void update(SkyAppBaseData data) {
        // TODO Auto-generated method stub
        super.update(data);
        SkyListStatusData statusData = (SkyListStatusData) data;
        this.status = statusData.getStatus();
        if (listType == ListType.APK_SCAN_LIST) {
            Drawable drawable = ((APKData) data).getIcon();
            try {
                cornerBitmap = ImageUtils.getRoundedCornerBitmap(((BitmapDrawable) drawable).getBitmap(), 25);
                iconImage.setImageBitmap(cornerBitmap);
            } catch (Exception e) {
                iconImage.setImageDrawable(drawable);
            }
            String version = ((APKData) data).getVersionName();
            if (version != null && version.trim().length() > 0) {
                if (sndInfoText == null) {
                    sndInfoText = UICommon.newTextView(mContext, infoTextSize, R.color.appInfoColor);
                }
                if (sndInfoText.getParent() == null) {
                    rightLayout.addView(sndInfoText);
                    rightLayout.setGravity(Gravity.TOP | Gravity.LEFT);
                }
                sndInfoText.setText(version);
                sndInfoText.setVisibility(View.VISIBLE);
            } else {
                if (sndInfoText != null)
                    sndInfoText.setVisibility(View.GONE);
            }
        }
        refreshStatus();
    }

    private void refreshStatus() {
        SkyListStatusData statusData = (SkyListStatusData) data;
        boolean showStatus = false;
        LogUtils.d("ListStatusBlock", "sunny refreshStatus:" + status.toString() + ";" + listType + ";" + installedText + ";" + statusText + ";" + statusData.getStatus());

        if (installedText == null) {
            installedText = UICommon.newTextView(mContext, installedTextSize, R.color.defaultColor);
        }
        installedText.setBackgroundResource(R.drawable.list_installed_bg);
        installedText.setText(R.string.installed);
        installedText.setWidth(SkyConstants.Div(89));
        installedText.setHeight(SkyConstants.Div(43));
        installedText.setGravity(Gravity.CENTER);
        if (installedText.getParent() == null) {
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(SkyConstants.Div(89), SkyConstants.Div(43), Gravity.BOTTOM | Gravity.RIGHT);
            addView(installedText, params);
        }

        if (status != AppStatus.NOT_DOWNLOADED) {
            if (listType == ListType.CGY_APP_LIST || listType == ListType.DOWNLOAD_LIST || listType == ListType.UPGRADE_APP_LIST)
                showStatus = true;
            if (listType == ListType.APK_SCAN_LIST) {
                if (status == AppStatus.INSTALLED || status == AppStatus.INSTALLING || status == AppStatus.HAS_NEW_VERSION || status == AppStatus.INSTALLED_WITH_CAN_NOT_BE_UNINSTALLED || status == AppStatus.HAS_NEW_VERSION_WITH_CAN_NOT_BE_UNINSTALLED)
                    showStatus = true;
            }
        }
        if (showStatus) {
            if (statusText == null) {
                statusText = UICommon.newTextView(mContext, infoTextSize, R.color.appInfoStatusColor);
            }
            if (statusText.getParent() == null) {
                rightLayout.addView(statusText);
                rightLayout.setGravity(Gravity.TOP | Gravity.LEFT);
            }
            if ((status == AppStatus.INSTALLED_WITH_CAN_NOT_BE_UNINSTALLED || status == AppStatus.INSTALLED)
                    && (listType == ListType.CGY_APP_LIST || listType == ListType.APK_SCAN_LIST)) {
                if (statusText != null)
                    statusText.setVisibility(View.GONE);
                if (installedText != null) {
                    installedText.setText(R.string.installed);
                    installedText.setVisibility(View.VISIBLE);
                }
            }
            else if((status == AppStatus.HAS_NEW_VERSION_WITH_CAN_NOT_BE_UNINSTALLED || status == AppStatus.HAS_NEW_VERSION)
                    && (listType == ListType.CGY_APP_LIST || listType == ListType.APK_SCAN_LIST)){
                if (statusText != null)
                    statusText.setVisibility(View.GONE);
                if (installedText != null) {
                    installedText.setText(R.string.has_new_version);
                    installedText.setVisibility(View.VISIBLE);
                }
            }else {
                if (installedText != null) {
                    installedText.setVisibility(View.GONE);
                }
                statusText.setText(getStrIdByStatus(status));
                statusText.setVisibility(View.VISIBLE);
            }


            LogUtils.d("detail", "sunny updateStatus:" + status.toString() + ";" + listType);
        } else {
            if (statusText != null)
                statusText.setVisibility(View.GONE);
            if (installedText != null) {
                installedText.setVisibility(View.GONE);
            }
            if (statusData.getShowInfo() == null || statusData.getShowInfo().trim().length() == 0) {
                rightLayout.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
            }
        }
    }

    protected int getStrIdByStatus(AppStatus status) {
        int strId = 0;
        switch (status) {
            case DOWNLOADING:
                strId = R.string.downloading;
                break;
            case DOWNLOAD_PAUSED:
                strId = R.string.paused;
                break;
            case DOWNLOADED:
                strId = R.string.downloaded;
                break;
            case WAITING:
                strId = R.string.waiting;
                break;
            case INSTALLING:
                strId = R.string.installing;
                break;
            case INSTALLED:
            case INSTALLED_WITH_CAN_NOT_BE_UNINSTALLED:
                strId = R.string.installed;
                break;
            case HAS_NEW_VERSION:
            case HAS_NEW_VERSION_WITH_CAN_NOT_BE_UNINSTALLED:
                strId = R.string.has_new_version;
                break;
        }
        return strId;
    }

    @Override
    public void onClick(View v) {
        if (inSelectionMode) {
            super.onClick(v);
        } else {
            if (data == null || (listType != ListType.APK_SCAN_LIST && !LiteAppApplication.checkNetworkConnection(TipToast.showNetIsNotConnectedToastRunnable)))
                return;

            if (clickListener != null)
                clickListener.onBlockClicked(this, data, mIndex);

            String strId = this.data.getId();
            if (strId == null || strId.trim().length() == 0)
                return;
            LogUtils.d("detail", "onClick, data = " + data.toString());
            Intent intent = new Intent(mContext, AppDetailActivity.class);
            intent.putExtra("id", strId);
            intent.putExtra("pkgName", "PPPPPPPPP");
//            mContext.startActivity(intent);
            HashMap<String,String> map = new HashMap<String,String>();
            SkyStartActivity.startActivity(mContext, mContext.getClass().getName(), intent, map, false);
        }
    }

    public void updateStatus(AppStatus status) {
        this.status = status;
        ((SkyListStatusData) data).setStatus(status);
        LogUtils.d("detail", "sunny updateStatus:" + status.toString());
        refreshStatus();
    }

    @Override
    public void clearData() {
        // TODO Auto-generated method stub
        super.clearData();
        if (statusText != null) {
            statusText.setText("");
            statusText.setVisibility(View.GONE);
        }
    }
}
